package com.xxl.job.admin.core.conf;

import com.xxl.job.admin.core.model.XxlJobInfo;
import com.xxl.job.admin.core.scheduler.XxlJobScheduler;
import com.xxl.job.admin.dao.XxlJobGroupDao;
import com.xxl.job.admin.dao.XxlJobInfoDao;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @Author: Zhihj
 * @Date: 2023-10-12 22:39:51
 */
@Component
public class XxlJobAdminConfig implements InitializingBean,DisposableBean {

    private static XxlJobAdminConfig adminConfig= null;

    public static XxlJobAdminConfig getAdminConfig(){
        return adminConfig;
    }

    private XxlJobScheduler xxlJobScheduler;

    @Override
    public void afterPropertiesSet() throws Exception {
        //在程序加载过程中，会调用此方法，然后加载init()方法
        adminConfig = this;
        xxlJobScheduler = new XxlJobScheduler();
        xxlJobScheduler.init();
    }
    @Override
    public void destroy() throws Exception {
        //程序结束时，调用destory方法，销毁init的线程
        xxlJobScheduler.destroy();
    }
    @Resource
    private XxlJobInfoDao xxlJobInfoDao;

    @Resource
    private XxlJobGroupDao xxlJobGroupDao;

    @Value("${xxl.job.triggerpool.fast.max}")
    private int triggerPoolFastMax;

    @Value("${xxl.job.triggerpool.slow.max}")
    private int triggerPoolSlowMax;

    public XxlJobInfoDao getXxlJobInfoDao(){
        return xxlJobInfoDao;
    }

    public XxlJobGroupDao getXxlJobGroupDao(){
        return xxlJobGroupDao;
    }

    public int getTriggerPoolFastMax() {
        if (triggerPoolFastMax < 200) {
            return 200;
        }
        return triggerPoolFastMax;
    }

    public int getTriggerPoolSlowMax() {
        if (triggerPoolSlowMax < 100) {
            return 100;
        }
        return triggerPoolSlowMax;
    }


}
